package com.lzh.runke.controller.exceltest.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.lzh.runke.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * easyExcel
 */
@Slf4j
@RestController
@RequestMapping("/easyExcel")
public class EasyExcelController {
//
//    @RequestMapping("/import")
//    public void import(HttpServletResponse response) {
//        String filename = "test.xlsx";
//        ArrayList<BaseExportEntity> list = new ArrayList<>();
//        ExportUtils.exportExcel(list, filename, null, response);
//    }


    @Resource
    private ResourceLoader resourceLoader;

    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        String filename = "test.xlsx";
        ArrayList<BaseExportEntity> list = new ArrayList<>();
        ExportUtils.exportExcel(list, filename, null, response);
    }

    @GetMapping("/exportToLocal")
    public void exportToLocal(HttpServletResponse response) throws IOException {
        // 模板文件路径
        String templateFilePath = "classpath:template/view/excel//template.xlsx";
        InputStream inputStream = ResourceLoader.class.getClassLoader().getResourceAsStream(templateFilePath);
        // 指定导出文件的路径
        String outputFilePath = "E:/IDEA/testt/just-do-it/from-runke/src/main/resources/users.xlsx";

        // 创建用户列表
        List<User> userList = new ArrayList<>();
        userList.add(new User("张三", 25));
        userList.add(new User("李四", 30));
        userList.add(new User("王五", 28));


        // 读取模板文件
        InputStream templateInputStream = new FileInputStream(new File(templateFilePath));

        // 创建输出文件
        FileOutputStream outputStream = new FileOutputStream(outputFilePath);

        // 使用 EasyExcel 根据模板导出数据
        EasyExcel.write(outputStream, User.class)
                .withTemplate(templateInputStream)
                .sheet()
                .doWrite(userList);

        // 关闭流
        templateInputStream.close();
        outputStream.close();

        System.out.println("导出成功！");
    }

    public static void main(String[] args) throws IOException {
        // 模板文件路径
        String templateFilePath = "classpath:template/view/excel//template.xlsx";
        InputStream inputStream = ResourceLoader.class.getClassLoader().getResourceAsStream(templateFilePath);
        // 指定导出文件的路径
        String outputFilePath = "E:/IDEA/testt/just-do-it/from-runke/src/main/resources/users.xlsx";

        // 创建用户列表
        List<User> userList = new ArrayList<>();
        userList.add(new User("张三", 25));
        userList.add(new User("李四", 30));
        userList.add(new User("王五", 28));


        // 读取模板文件
        InputStream templateInputStream = new FileInputStream(new File(templateFilePath));

        // 创建输出文件
        FileOutputStream outputStream = new FileOutputStream(outputFilePath);

        // 使用 EasyExcel 根据模板导出数据
        EasyExcel.write(outputStream, User.class)
                .withTemplate(templateInputStream)
                .sheet()
                .doWrite(userList);

        // 关闭流
        templateInputStream.close();
        outputStream.close();

        System.out.println("导出成功！");
    }


}
